package com.jinxixin.android.recordvideo.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView {
    private static final String TAG = "MySurfaceView";

    public MySurfaceView(Context context) {
        super(context);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    //手指按下的点为(x1, y1)手指离开屏幕的点为(x2, y2)
    float x1 = 0;
    float x2 = 0;
    float y1 = 0;
    float y2 = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //当手指按下的时候
            x1 = event.getX();
            y1 = event.getY();
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            //当手指离开的时候
            x2 = event.getX();
            y2 = event.getY();
            float x = x2 - x1;
            float minMove = 120; // 最小滑动距离
            if (x > 0 && Math.abs(x) > minMove) {
                //向右滑
                mOnSlidingEventsListener.rightMove();
            } else if (x < 0 && Math.abs(x) > minMove) {
                //向左滑
                mOnSlidingEventsListener.leftMove();
            }
        }
        return super.onTouchEvent(event);
    }

    //滑动事件监听
    private OnSlidingEventsListener mOnSlidingEventsListener;

    public void setOnSlidingEventsListener(OnSlidingEventsListener onSlidingEventsListener) {
        mOnSlidingEventsListener = onSlidingEventsListener;
    }

    public interface OnSlidingEventsListener {
        void leftMove();

        void rightMove();
    }
}
